package com.ke.jun.view.utils

import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Camera
import android.util.TypedValue
import androidx.annotation.DrawableRes


val Float.dp: Float
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this,
        Resources.getSystem().displayMetrics
    )


val Int.dp: Float
    get() = this.toFloat().dp


val Float.sp: Float
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_SP,
        this,
        Resources.getSystem().displayMetrics
    )

val Int.sp: Float
    get() = this.toFloat().sp


//头像
fun Context.getAvatar(@DrawableRes image: Int, targetWidth: Int): Bitmap {
    val options = BitmapFactory.Options()
    options.inJustDecodeBounds = true
    BitmapFactory.decodeResource(resources, image, options)
    options.inJustDecodeBounds = false
    options.inDensity = options.outWidth
    options.inTargetDensity = targetWidth
    return BitmapFactory.decodeResource(resources, image, options)
}

/**
 * inline fun Canvas.withSave(block: Canvas.() -> Unit) {
val checkpoint = save()
try {
block()
} finally {
restoreToCount(checkpoint)
}
}
 */

inline fun Camera.withSave(block: (Camera) -> Unit) {
    save()
    try {
        block(this)
    } finally {
        restore()
    }
}